
// Compare two strategies 
var STRATSTRATCMP = new Array();
STRATSTRATCMP["XX"]="=";STRATSTRATCMP["XY"]=">";STRATSTRATCMP["XA"]=">";STRATSTRATCMP["XB"]=">";STRATSTRATCMP["X0"]=">";
STRATSTRATCMP["YX"]="<";STRATSTRATCMP["YY"]="=";STRATSTRATCMP["YA"]=">";STRATSTRATCMP["YB"]=">";STRATSTRATCMP["Y0"]=">";
STRATSTRATCMP["AX"]="<";STRATSTRATCMP["AY"]="<";STRATSTRATCMP["AA"]="=";STRATSTRATCMP["AB"]="=";STRATSTRATCMP["A0"]="=";
STRATSTRATCMP["BX"]="<";STRATSTRATCMP["BY"]="<";STRATSTRATCMP["BA"]="=";STRATSTRATCMP["BB"]="=";STRATSTRATCMP["B0"]="=";
STRATSTRATCMP["CX"]="<";STRATSTRATCMP["CY"]="<";STRATSTRATCMP["CA"]="=";STRATSTRATCMP["CB"]="=";STRATSTRATCMP["C0"]="=";
STRATSTRATCMP["0X"]="<";STRATSTRATCMP["0Y"]="<";STRATSTRATCMP["0A"]="=";STRATSTRATCMP["0B"]="=";STRATSTRATCMP["00"]="=";
STRATSTRATCMP["XC"]=">";STRATSTRATCMP["YC"]=">";STRATSTRATCMP["AC"]="=";STRATSTRATCMP["BC"]="=";STRATSTRATCMP["0C"]="=";

// Compare two document values 
var VALVALCMP = new Array();
VALVALCMP["XNXN"]="=";VALVALCMP["XNXI"]=">";VALVALCMP["XNY"]=">";VALVALCMP["XNA"]=">";VALVALCMP["XN0"]=">";
VALVALCMP["XIXN"]="<";VALVALCMP["XIXI"]="=";VALVALCMP["XIY"]=">";VALVALCMP["XIA"]=">";VALVALCMP["XI0"]=">";
VALVALCMP["YXN"] ="<";VALVALCMP["YXI" ]="<";VALVALCMP["YY"] ="=";VALVALCMP["YA"] =">";VALVALCMP["Y0"] =">";
VALVALCMP["AXN"] ="<";VALVALCMP["AXI"] ="<";VALVALCMP["AY"] ="<";VALVALCMP["AA"]="=" ;VALVALCMP["A0"] ="=";
VALVALCMP["0XN"] ="<";VALVALCMP["0XI"] ="<";VALVALCMP["0Y"] ="<";VALVALCMP["0A"]="=" ;VALVALCMP["00"] ="=";

// Compare strategy and value 
var STRATVALCMP = new Array();
STRATVALCMP["XX"]="=";STRATVALCMP["XY"]="=";STRATVALCMP["XA"]=">";STRATVALCMP["XB"]=">";STRATVALCMP["X0"]=">";
STRATVALCMP["YX"]="=";STRATVALCMP["YY"]="=";STRATVALCMP["YA"]=">";STRATVALCMP["YB"]=">";STRATVALCMP["Y0"]=">";
STRATVALCMP["AX"]="<";STRATVALCMP["AY"]="<";STRATVALCMP["AA"]="=";STRATVALCMP["AB"]="=";STRATVALCMP["A0"]=">";
STRATVALCMP["BX"]="<";STRATVALCMP["BY"]="<";STRATVALCMP["BA"]="=";STRATVALCMP["BB"]="=";STRATVALCMP["B0"]=">";
STRATVALCMP["0X"]="<";STRATVALCMP["0Y"]="<";STRATVALCMP["0A"]="<";STRATVALCMP["0B"]="<";STRATVALCMP["00"]="=";


function strat2val(cat) {
  if(cat=="B") {
	return "A";
  }
  return cat;
}


// compare doc cats for cl1 and cl2 (cl1 <= cl2 ) ?

function catcmp(cat1,cat2) {
  return STRATSTRATCMP[(cat1||"0")+(cat2||"0")];
}

function valcmp(cat1,cat2) {
  return VALVALCMP[(cat1||"0")+(cat2||"0")];
}

function catvalcmp(cat1, val1) {
  return STRATVALCMP[(cat1||"0")+(val1||"0")];
}


function catmin(cat1,cat2) {
  return (catcmp(cat1,cat2)==">") ? cat2 : cat1;
}

function valmin(cat1,cat2) {
  return (valcmp(cat1,cat2)==">") ? cat2 : cat1;
}


// compare doc cats for cl1 and cl2 (cl1 <= cl2 ) ?
function is_catlessorequal(cat1,cat2) {
  return catcmp(cat1,cat2) != ">";
}

function is_vallessorequal(cat1,cat2) {
  return valcmp(cat1,cat2) != ">";
}


// check if newcat does not conflict with upper claims 
function check_strat_integrity(cl,pn,newcat) {
  var dep=GetClaimDep(cl);
  if(pn!="" && is_cldependent(dep)) { // ignore deletions   
    var depcat=GetClaimDocStrat(dep,pn);          // check with upper claim  
    var depdoc=GetClaimDocCat(dep,newcat);     // check if up is same doc 
    if((catcmp(newcat,depcat)==">") || (pn != depdoc && (newcat=="X" || newcat=="Y" ))) {	
      //alert("A depdoc:"+depdoc+" pn:"+pn+" newcat: "+newcat+" depcat "+depcat);        
      if(newcat=="Y" && catcmp("Y",GetClaimDocVal(dep,pn))!=">") { // up is X for same doc 
	return true;
      }      
      return false;
    }
  }
  return true;
}

// check if newval does not conflict with upper claims 
function check_val_integrity(cl,pn,newval) {
  var dep=GetClaimDep(cl);
  if(is_cldependent(cl)) {    
    if(GetClaimGroup(cl) == GetClaimGroup(dep)) {
      var eq=GetClaimEq(dep);
      var depval=GetClaimDocVal(dep,pn,eq);
      //alert("dep "+dep+"="+eq+" val "+depval+" >= "+newval);
      if(valcmp(newval,depval)==">") { // if upper claim is less, error
	return false;
      }
    }
  }
  return true;
}
